package com.gx.ObserverPattern.impl;

import com.gx.ObserverPattern.Observer;
import com.gx.ObserverPattern.Subject;

import java.util.ArrayList;


public class WeatherData implements Subject {
	private static  ArrayList<Observer> observers;


	private float temperature;
	private float humidity;
	private float pressure;
	public WeatherData() {
		observers = new ArrayList();
	}

	@Override
	public void registerObserver(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		int i = observers.indexOf(observer);
		if (i > 0) {
			observers.remove(i);
		}
	}

	@Override
	public void notifyObserver() {
		for (Observer observer : observers) {
			observer.update(temperature, humidity, pressure);
		}
	}

	public void measurementsChanged() {
		notifyObserver();
	}
	
	
	public void setMeasurements(float temperature,float humidity,float pressure){
		this.pressure=pressure;
		this.humidity=humidity;
		this.temperature=temperature;
		measurementsChanged();
	}

}
